import xmlrpclib
from SimpleXMLRPCServer import SimpleXMLRPCServer
import bsddb
import struct

class Method:
    def __init__(self):
        self.urlid = bsddb.btopen('../db.url-id','r')
        self.idurl = bsddb.btopen('../db.id-url','r')
        self.title = bsddb.btopen('../db.title','r')
        self.binfile = open('../db.webgraph','rb')
        self.webgraph = {}
# read webgraph
        intsize = struct.calcsize('i')
        tmp = 0
        while 1:
            data = self.binfile.read(intsize)
            if data == '':
                break
            src = str(struct.unpack('i',data)[0])

            data = self.binfile.read(intsize)
            if data == '':
                break
            outdegree = str(struct.unpack('i',data)[0])
            dest = []
            for i in range(int(outdegree)):
                data = self.binfile.read(intsize)
                if data == '':
                    break
                dest.append(str(struct.unpack('i',data)[0]))
            self.webgraph[str(src)] = dest
        self.binfile.close()

    def site(self,site):
        for i in range(5):
            print self.urlid.next()
        ans = ''        
        if self.urlid.has_key(site) :
            siteid = self.urlid[site]
            ans += '>> URL  : '+site+'\n'
            if self.title.has_key(siteid) :
                t = self.title[siteid]
                ans += '>> Title: '+t+ '\n'
            ans += '\n'

            if self.webgraph.has_key(siteid) :
                dest = self.webgraph[siteid]
                for d in dest:
                    url = self.idurl[d]
                    ans += url +'\n'
                    if self.title.has_key(d) :
                        ans += self.title[d] + '\n'
                    ans += '----\n'
        else:
            ans += 'site not found\n'
        print ans
        return ans
    def __del__(self):
        self.idurl.close()
        self.urlid.close()
        self.title.close()

try:
    server = SimpleXMLRPCServer(('localhost',9999))
    print 'Listening on port 10000...'
    server.register_instance(Method())
    server.serve_forever()
except KeyboardInterrupt:
    print 'Closed Connection'
# End
